package com.firebird.app.kotlin.stock.ui.index_fragment.index

import android.os.Bundle
import android.support.design.widget.AppBarLayout
import android.support.design.widget.CollapsingToolbarLayout
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import butterknife.BindView
import butterknife.OnClick
import butterknife.Unbinder
import com.chad.library.adapter.base.BaseQuickAdapter
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.Config
import com.firebird.app.kotlin.stock.TabAdapter
import com.firebird.app.kotlin.stock.base.BaseMvpFragment
import com.firebird.app.kotlin.stock.data.IndexMenuBean
import com.firebird.app.kotlin.stock.ui.allocation_manage.AllocationManageActivity
import com.firebird.app.kotlin.stock.ui.allocation_plan.AllocationPlanActivity
import com.firebird.app.kotlin.stock.ui.index_fragment.index.contract.IndexFragmentContract
import com.firebird.app.kotlin.stock.ui.index_fragment.index.fragment.dayhot.DayHot
import com.firebird.app.kotlin.stock.ui.index_fragment.index.fragment.informationcenter.InformationCenter
import com.firebird.app.kotlin.stock.ui.index_fragment.index.presenter.IndexFragmentPresenter
import com.firebird.app.kotlin.stock.ui.runing.RunningActivity
import com.firebird.app.kotlin.stock.ui.search.SearchActivity
import java.util.*


class IndexFragment : BaseMvpFragment<IndexFragmentPresenter?>(), IndexFragmentContract.View {
    @BindView(R.id.xtab)
    var xtab: TabLayout? = null
    @BindView(R.id.viewpager)
    var viewpager: ViewPager? = null
    var unbinder: Unbinder? = null
    @BindView(R.id.recycler)
    var recycler: RecyclerView? = null
    var unbinder1: Unbinder? = null
    @BindView(R.id.tvs_search)
    var tvsSearch: TextView? = null
    var unbinder2: Unbinder? = null
    private var titleList: MutableList<String>? = null
    private var fragmentList: MutableList<Fragment>? = null
    private val list: MutableList<IndexMenuBean> = ArrayList<IndexMenuBean>()
    private var adapter: IndexMenuAdapter? = null
    private val bundle = Bundle()
    override fun initView(view: View?) {
        titleList = ArrayList()
        titleList.add("今日热点")
        titleList.add("资讯中心")
        fragmentList = ArrayList<Fragment>()
        fragmentList.add(DayHot())
        fragmentList.add(InformationCenter())
        viewpager!!.adapter = TabAdapter(activity!!.supportFragmentManager, titleList, fragmentList)
        viewpager!!.offscreenPageLimit = 0
        xtab.setupWithViewPager(viewpager)
        val tabAt: TabLayout.Tab = xtab.getTabAt(0)
        tabChangSize(tabAt)
        viewpager!!.currentItem = 0
        xtab.addOnTabSelectedListener(object : OnTabSelectedListener() {
            fun onTabSelected(tab: TabLayout.Tab) {
                viewpager!!.currentItem = tab.getPosition()
                tabChangSize(tab)
            }

            fun onTabUnselected(tab: TabLayout.Tab) {
                tab.setCustomView(null)
            }

            fun onTabReselected(tab: TabLayout.Tab?) {}
        })
        setData()
        adapter = IndexMenuAdapter(R.layout.item_index_menu, list)
        recycler.setLayoutManager(GridLayoutManager(activity, 4, LinearLayoutManager.VERTICAL, false))
        recycler.setAdapter(adapter)
        adapter.setOnItemClickListener(object : OnItemClickListener() {
            fun onItemClick(adapter: BaseQuickAdapter?, view: View?, position: Int) {
                when (position) {
                    0 -> {
                        //免息
                        bundle.putInt("mIndex", Config.FREE)
                        toClass(activity!!, AllocationPlanActivity::class.java, bundle)
                    }
                    1 -> {
                        //按日
                        bundle.putInt("mIndex", Config.DAY)
                        toClass(activity!!, AllocationPlanActivity::class.java, bundle)
                    }
                    2 -> {
                        //按月
                        bundle.putInt("mIndex", Config.MON)
                        toClass(activity!!, AllocationPlanActivity::class.java, bundle)
                    }
                    3 ->  //资金流水
                        toClass(activity!!, RunningActivity::class.java)
                    4 ->  //配资管理
                        toClass(activity!!, AllocationManageActivity::class.java)
                }
            }
        })
    }

    override val layoutId: Int
        protected get() = R.layout.fragment_index

    override fun showLoading() {}
    override fun hideLoading() {}
    override fun onError(throwable: Throwable?) {}
    private fun setData() {
        val bean1 = IndexMenuBean()
        val bean2 = IndexMenuBean()
        val bean3 = IndexMenuBean()
        val bean4 = IndexMenuBean()
        val bean5 = IndexMenuBean()
        val bean6 = IndexMenuBean()
        val bean7 = IndexMenuBean()
        bean1.setmIcon(R.mipmap.icon_mianxi)
        bean1.setmText("免息配资")
        bean2.setmIcon(R.mipmap.icon_day)
        bean2.setmText("按日配资")
        bean3.setmIcon(R.mipmap.icon_month)
        bean3.setmText("按月配资")
        bean4.setmIcon(R.mipmap.icon_liushui)
        bean4.setmText("资金流水")
        bean5.setmIcon(R.mipmap.icon_manage)
        bean5.setmText("配资管理")
        bean6.setmIcon(R.mipmap.icon_tuiguang)
        bean6.setmText("推广")
        bean7.setmIcon(R.mipmap.icon_more)
        bean7.setmText("更多")
        list.add(bean1)
        list.add(bean2)
        list.add(bean3)
        list.add(bean4)
        list.add(bean5)
        list.add(bean6)
        list.add(bean7)
    }

    //tab选中是改变字体大小
    private fun tabChangSize(tab: TabLayout.Tab) {
        val textView = TextView(activity)
        val selectedSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 18f, resources.displayMetrics)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, selectedSize)
        val paint = textView.paint
        paint.isFakeBoldText = true
        textView.setText(tab.getText())
        tab.setCustomView(textView)
    }

    @OnClick(R.id.tvs_search)
    fun onViewClicked() {
        toClass(activity!!, SearchActivity::class.java)
    }
}